/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package evidence.model.modeloveTridy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Protheo
 */
public class Ukony implements Serializable, Runnable {
    public static final long serialVersionUID = 1;
    private Map<String, Ukon> seznamUkonu;
    private File soubor;

    public Ukony(String ukonyCesta) {
        ObjectInputStream is = null;
        try {
            soubor = new File(ukonyCesta);
            if (!soubor.exists()) {
                soubor = new File("userdata" + File.separator + "ukony.sou");
                seznamUkonu = new HashMap<String, Ukon>();
                try {
                    soubor.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
                }
                save();
            }
            is = new ObjectInputStream(new FileInputStream(soubor));
            seznamUkonu = (Map<String, Ukon>) is.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addUkon(String ukon){
        if (!this.isUkon(ukon)){
            this.seznamUkonu.put(ukon, new Ukon(ukon));
        }
    }

    public void addUkon(String ukon, int cisloKlienta, int typ){
        this.addUkon(ukon);
        this.seznamUkonu.get(ukon).addKlient(cisloKlienta);
        this.seznamUkonu.get(ukon).setTyp(typ);
    }

    public void removeUkon(String ukon){

    }

    public void removeUkon(String ukon, int cisloKlienta){

    }

    public Boolean isUkon(String ukon){
        if(this.seznamUkonu.containsKey(ukon)){
            return true;
        }
        return false;
    }

    public List<String> getUkony(int cisloKlienta){
        List<String> ukonyKlienta = new ArrayList<String>();
        for (Iterator<String> it = this.seznamUkonu.keySet().iterator(); it.hasNext();) {
            String string = it.next();
            if(this.seznamUkonu.get(string).getKlienti().contains(cisloKlienta)){
                ukonyKlienta.add(string);
            }
        }
        Collections.sort(ukonyKlienta);
        return ukonyKlienta;
    }

    public Set<Integer> getTypy(String ukon){
        try {
            return this.seznamUkonu.get(ukon).getTyp();
        } catch (NullPointerException ex){
            return new HashSet<Integer>(1);
        }
    }

    public final void save(){
        ObjectOutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(soubor));
            os.writeObject(seznamUkonu);
            os.flush();
            os.close();
        } catch (IOException ex) {
            Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                os.close();
            } catch (IOException ex) {
                Logger.getLogger(Ukony.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String toString() {
        return "Ukony{" + "seznamUkonu=" + seznamUkonu + '}';
    }
}
